/**
 * 自定义函数
 * */

var userLogin = 'userLogin'; //用户信息
var routes = {
    index: '/h5/index.html', //入口路由
    indexno: '/h5/index-no.html', //入口路由
    scene: '/h5/scene.html', //场景路由
    tmpOne: '/h5/tmp.html', //模板详情路由
}; //前端路由地址
var apiDomain = '/api/h5';
var apis = {
    toh5code: { //更新H5信息、扫码记录
        method: 'post',
        url: apiDomain + '/user/toh5code'
    },
    sceneIndex: { //场景列表
        method: 'post',
        url: apiDomain + '/scene/index'
    },
    sceneCntIndex: { //场景内容列表
        method: 'post',
        url: apiDomain + '/scene/cnt/index'
    },
    tmpOne: { //场景详情
        method: 'post',
        url: apiDomain + '/tmp/one'
    },
    tmpReplyIndex: { //场景评论列表
        method: 'post',
        url: apiDomain + '/tmp/reply/list'
    },
    tmpReplyAdd: { //场景评论新增
        method: 'post',
        url: apiDomain + '/tmp/reply/add'
    },
    tmpFollowAdd: { //场景关注设置
        method: 'post',
        url: apiDomain + '/tmp/follow/add'
    },
    tmpAdUse: { //广告
        method: 'post',
        url: apiDomain + '/tmp/ad/use'
    },
}; //后端接口地址
var pageSize = 10;



////登录弹窗
//function loginForm(){
//    var h = '';
//    h += '<style type="text/css">#formMask { max-width: 500px; width: 100%; height: 100vh; background: #000; opacity: 0.2; position: fixed; top: 0; } #formOut { margin: 10px 10%; padding: 5%; width: 70%; background: #fff; border: 1px solid #ccc; border-radius: 10px; box-shadow: 0 0 10px #ccc; position: fixed; top: 20%; } #formOut .group { margin: 20px 0; } #formOut input { margin-top: 10px; padding: 10px 5%; width: 90%; border: 1px solid #0088cc; border-radius: 5px; font-size: 16px; } #formOut button { padding: 10px 0; width: 100%; background: #0088CC; color: #fff; border: 0; border-radius: 5px; }</style>';
//    h += '<div id="formMask"></div>';
//    h += '<div id="formOut">';
//    h += '<div class="group">';
//    h += '输入您的手机号：';
//    h += '<input type="text" placeholder="输入手机号" id="phone" value=""/>';
//    h += '</div>';
//    h += '<div class="group">';
//    h += '<button type="button" onclick="loginByPhone();">确定</button>';
//    h += '</div>';
//    h += '</div>';
//    h += '';
//    $('#formPop').html(h);
//}


////设置手机号
//function loginByPhone(){
//    var h = '';
//    if (code == undefined || code == '') {
//        h = '<div style="margin-top: 50%; width: ' + cw + 'px; height: ' + ch + 'px; text-align: center; color: #888;">';
//        h += '无效码...';
//        h += '</div>';
//        $('body').html(h);
//        return
//    }
//    var phone = $('#phone').val();
//    if (phone == '') {
//        msgPop({msg: '手机号必填'});
//        return
//    }
//    var formData = {
//        phone: phone,
//        code: code
//    };
//    $.ajax({
//        type: apis.userPhone.method,
//        url: apis.userPhone.url,
//        data: formData,
//        success: function (res) {
//            console.log(res);
//            if (res.code != 0) {
//                msgPop({msg: res.msg});
//                return
//            }
//            var userInfo = {
//                token: res.data.token,
//                uid: res.data.uid,
//                code: res.data.code,
//                phone: phone,
//                h5code: res.data.h5code,
//            };
//            setUserInfo({val: userInfo}); //设置本地缓存
//            var url = window.location.href;
//            var httpUrls = url.split('?');
//            var code = getParamByUrl('code');
//            window.location.href = httpUrls[0] + '?code=' + code +'&token=' + res.data.token;
//        },
//        error: function () {
//            console.log('error');
//        }
//    });
//}


////小程序弹窗
//function appPop(){
//    var userInfo = getUserInfo();
//    if (!userInfo || !userInfo.h5code) {
//        msgPop({msg: '未登录'});
//        setTimeout(function(){
//            loginForm();
//        }, 500);
//        return
//    }
//    window.location.href = '/h5/views/toxcx.html';
//}


//红包小费
function tipPop(){
    var code = getParamByUrl('code');
    var h5code = getParamByUrl('h5code');
    var scene_id = getParamByUrl('scene_id');
    var tourl = 'toxcx.html?code=' + code + '&h5code=' + h5code + '&scene_id=' + scene_id;
    var h = '';
    h += '<style type="text/css"> #tipMask { width: 100vw; height: 100vh; background: #000; opacity: 0.5; position: fixed; top: 0; left: 0; z-index: 100; } #tipCnt { width: 80vw; max-width: 500px; height: 50vh; background: rgb(226,84,75); border-radius: 10px; overflow: hidden; box-shadow: 0 0 5px red;; position: fixed; top: 20vh; left: 10vw; z-index: 100; } #tipCnt .cnt { padding: 30% 0; padding-bottom: 0; color: orange; text-align: center; font-weight: bolder; position: relative; } #tipCnt .brbtm { width: 100%; height: 100px; border: 0; border-bottom: 5px solid orange; border-radius: 100%; position: relative; top: -30px; } #tipCnt .kai { margin: 0 auto;  width: 60px; height: 60px; background: orange; border: 2px solid orangered; border-radius: 100px; box-shadow: 0 0 5px #000; text-align: center; color: #000; font-size: 25px; font-weight: bolder; line-height: 60px; position: relative; top: -60px; } </style>';
    h += '<div id="tipMask"></div>';
    h += '<div id="tipCnt">';
    h += '<div class="cnt">恭喜你收获红包 <br> 点击领取！</div>';
    h += '<div class="brbtm"></div>';
    h += '<div class="kai" onclick="window.location.href=\'' + tourl + '\';">開</div>';
    h += '</div>';
    h += '';
    $('#tipPop').html(h);
}


//小程序弹窗
function appPop(){
    var p = '/h5/toxcx.html';
    p += '?scene_id=' + scene_id;
    p += '&type=scene_cnt';
    p += '&code=' + code;
    p += '&h5code=' + h5code;
    window.location.href = p;
}


//显示前几个字符
function strPrefix(str, limit){
    if (str.length > limit) {
        return str.substring(0, limit) + '...';
    } else {
        return str;
    }
}


//显示后几个字符
function strSuffix(str, limit){
    if (str.length > limit) {
        return '...' + str.slice(- limit);
    } else {
        return str;
    }
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var cwb = cw * 0.5; cwb = cwb>500 ? 500 : cwb;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch - 35) + 'px; background: #000; opacity: 0.2; position: fixed; top: 35px; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//抓图
function uploadImage() {
    var img = $("#viewimg"), dataURL, fileObj = $('#uploadFile')[0],
        windowURL = window.URL || window.webkitURL, reader = new FileReader();
    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
    img.attr("src", dataURL); //显示图片
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        showImg(e.target.result);
    }
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//生成随机数
function randInt(min, max) {
    // 加1是因为我们想要包含max值在内
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


//获取登录信息
function getUserInfo(){
    var cache = window.localStorage.getItem(userLogin);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//设置登录信息
function setUserInfo(e){
    window.localStorage.setItem(userLogin, JSON.stringify(e.val));
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//设置缓存
function setCache(e){
    window.localStorage.setItem(e.key, JSON.stringify(e.val));
}